﻿package KK.net {
	import KK.Queue.IQueueObject;
	import KK.Queue.Queue;
	import KK.System.Pool.Pool;
	
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLVariables;
	import flash.utils.ByteArray;
	
	public class QueueLoader extends Queue{
		private var _urlLoader:Vector.<IQueueObject>;
		private var _swfLoader:Vector.<IQueueObject>;
		protected var _amfLoader:Vector.<IQueueObject>;
		protected var _bytesLoader:Vector.<IQueueObject>;
		static public const SWF:String="SWF_LOADER";
		static public const URL:String="URL_LOADER";
		
//		private static var Instance:QueueLoader;
		public function QueueLoader(max:int=1) {
			// constructor code
			/*
			if(Instance==null)
			{
				super(max);
			}else{
				throw new Error("this sington class pless using GetInstance function get class sington");
			}
			Instance=this;
			*/
			super(max);
			init();
		}
		private function init():void{
			_urlLoader=new Vector.<IQueueObject>;
			_swfLoader=new Vector.<IQueueObject>;
			_amfLoader=new Vector.<IQueueObject>;
			_bytesLoader=new Vector.<IQueueObject>;
		}
		override public function set Max(max:int):void{
			super.Max=max;
		}
		public function AddLoader(_url:String,_Complete:Function,_params:Object=null,_IOError:Function=null,_SecurityError:Function=null,contentType:String=null):int{
			var id:int=GetURLLoader();
			if(id==-1)
			{
				return -1;
			}
			var loader:Q_Loader=Q_Loader(_urlLoader[id]);
			loader.SetLoader(_url,_Complete,_params,_IOError,_SecurityError);
			loader.contentType=(contentType);
			AddQueue(loader);
			
			return id;
		}
		
		public function UpLoader(_url:String,_Complete:Function,_params:Object=null,_IOError:Function=null,_SecurityError:Function=null,contentType:String=null):int{
			var id:int=GetURLLoader();
			if(id==-1)
			{
				return -1;
			}
			var loader:Q_Loader=Q_Loader(_urlLoader[id]);
			loader.dataFormat=URLLoaderDataFormat.BINARY;
			loader.SetLoader(_url,_Complete,_params,_IOError,_SecurityError);
			loader.contentType=(contentType);
			AddQueue(loader);
			
			return id;
		}
		public function AddBytesLoader(byte:ByteArray,_Complete:Function,name:String="",_IOError:Function=null,_SecurityError:Function=null,_Progress:Function=null):int{
			var id:int=GetBytesLoader();
			if(id==-1)
			{
				return -1;
			}
			var loader:Q_SwfLoader=Q_SwfLoader(_bytesLoader[id]); 
			loader.SetBytesLoader(byte,_Complete,name,_IOError,_SecurityError,_Progress);
			AddQueue(loader);
			return id;
		}
		public function AddAMFLoader(_url:String,_path:String,_Complete:Function,_IOError:Function=null,_SecurityError:Function=null,..._params):int
		{
			var id:int=GetAMFLoader();
			if(id==-1)
			{
				return -1;
			}
			var loader:Q_AMFLoader=Q_AMFLoader(_amfLoader[id]); 
			if(_params.length>0)
			{
				loader.SetLoader.apply(null,[_url,_path,_Complete,_IOError,_SecurityError].concat(_params));
			}else{
				
				loader.SetLoader(_url,_path,_Complete); 
			}
			AddQueue(loader);
			
			return id;
		}
		public function AddSwfLoader(_url:String,_Complete:Function,_params:Object=null,_IOError:Function=null,_SecurityError:Function=null,_Progress:Function=null):int{
			var id:int=GetSwfLoader();
			if(id==-1)
			{
				return -1;
			}
			var loader:Q_SwfLoader=Q_SwfLoader(_swfLoader[id]); 
			loader.SetLoader(_url,_Complete,_params,_IOError,_SecurityError,_Progress);
			AddQueue(loader);
			return id;
		}
		private function GetSwfLoader():int{
			var i:int=-1;
			var loader:IQueueObject=null;
			for(i=0;i<_swfLoader.length;i++)
			{ 
				if(Q_SwfLoader(_swfLoader[i]).PoolStatus==Pool.UNUSED)
				{
					loader=_swfLoader[i];
					break;
				}
			}
			if(loader==null)
			{
				loader=new Q_SwfLoader();
				_swfLoader.push(loader);
				i=_swfLoader.length-1;
			}
			return i;
		}
		
		protected function GetBytesLoader():int{
			var i:int=-1;
			var loader:IQueueObject=null;
			for(i=0;i<_bytesLoader.length;i++)
			{ 
				if(_bytesLoader[i].Status==0)
				{
					loader=_bytesLoader[i];
					break;
				}
			}
			if(loader==null)
			{
				loader=new Q_SwfLoader();
				_bytesLoader.push(loader);
				i=_bytesLoader.length-1;
			}
			return i;
		}
		
		private function GetAMFLoader():int{
			
			var i:int=-1;
			var loader:IQueueObject=null;
			for(i=0;i<_amfLoader.length;i++)
			{ 
				if(_amfLoader[i].Status==0)
				{
					loader=_amfLoader[i];
					break;
				}
			}
			if(loader==null)
			{
				loader=new Q_AMFLoader();
				_amfLoader.push(loader);
				i=_amfLoader.length-1;
			}
			return i;
		}
		private function GetURLLoader():int{
			var i:int=-1;
			var loader:IQueueObject=null;
			for(i=0;i<_urlLoader.length;i++)
			{ 
				if(_urlLoader[i].Status==0)
				{
					loader=_urlLoader[i];
					break;
				}
			}
			if(loader==null)
			{
				loader=new Q_Loader(null,null);
				_urlLoader.push(loader);
				i=_urlLoader.length-1;
			}
			return i;
		}
		//删除指定id的任务,成功返回true,否则false
		public function RemoveID(id:int,type:String=URL):Boolean
		{
			var result:Boolean=false;
			var loader:IQueueObject=type==URL?_urlLoader[id]:_swfLoader[id];
			if(loader!=null)
			{
				result=RemoveQueue(loader);
			}else{
				result=false;
			}
			return result;
		}
		/*
		static public function GetInstance(max:int=3):QueueLoader
		{
			if(Instance==null)
			{
				new QueueLoader();
			}
			Instance.Max=max;
			return Instance;
		}
		*/
	}
	
}
